package com.project.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.bean.PageBean;
import com.project.bean.StudentBean;
import com.project.bean.UserBean;
import com.project.service.IFollowService;
import com.project.service.IPostService;
import com.project.service.IStudentService;
import com.project.service.IUserService;

@Controller
public class StudentController {
	@Autowired
private IStudentService stuService;
	@Autowired
private IPostService postService;
	@Autowired
	private IFollowService followService;
	@Autowired
	private IUserService userservice;
	/*
	 * @GetMapping("/student/{uid}") public String
	 * showOneStudent(@PathVariable("uid")Integer uId,ModelMap map) { StudentBean
	 * stu=stuService.findById(uId); map.addAttribute("stu", stu); return
	 * "redirect:/user_idex1.html"; }
	 */
	
	
	@RequestMapping("/student_idex_other")
	public String findStuById(ModelMap model,int sUId,HttpServletRequest req) {
		Integer uid=(Integer) req.getSession().getAttribute("user");	
		//System.out.println(sUId);
		StudentBean student=stuService.findById(sUId);
		//System.out.println(student);
		UserBean userBean=userservice.findUserById(uid);				
		PageBean posts=postService.findUserPostsByPage(sUId, 1, 5);
		
		model.addAttribute("posts", posts);
		model.addAttribute("student", student);
		model.addAttribute("user", userBean);
		return "/student_idex_other.html";
	}
	
	//添加关注
		@RequestMapping("/followStudent")
		@ResponseBody
		public int addFollow(HttpServletRequest req,int cUId) {
			Integer uid=(Integer) req.getSession().getAttribute("user");				
			//判断是否已经关注
			if (followService.findFollow(uid, cUId)>0) {
				return 1;
			}else {
				if (followService.addFollow(uid, cUId)>0) {
					return 2;
				}
			}		
			return 0; 

		}
}
